package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Person;
import com.ruoyi.system.domain.Ptesting;
import com.ruoyi.system.domain.vo.IndexIntVo;
import com.ruoyi.system.domain.vo.IndexVo;
import com.ruoyi.system.mapper.PersonMapper;
import com.ruoyi.system.mapper.PtestingMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IPersonService;
import com.ruoyi.system.service.IPtestingService;
import com.ruoyi.system.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @author ruoyi
 * @date 2022-04-21
 */
@RestController
@RequestMapping("/system/index")
public class IndexController extends BaseController {
    @Autowired
    private IndexService indexService;

    @Autowired
    private IPersonService personService;
    @Autowired
    private IPtestingService ptestingService;
    @Autowired
    private PtestingMapper ptestingMapper;



    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private PersonMapper personMapper;

    /**
     * 查询总人数，已检测人次
     */
    @GetMapping("/statistics")
    public Map statistics(Ptesting ptesting) {
        Map map = new HashMap();
        map.put("renshu", personMapper.selectPersonCount(getDeptId())); //总统人数
        ptesting.setDeptId(getDeptId());
        map.put("renci", ptestingMapper.selectPtestingCount(ptesting));  //总人次
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("data", map);
        stringObjectHashMap.put("cod", 200);
        stringObjectHashMap.put("msg", "查询成功");
        return stringObjectHashMap;
    }

    /**
     * 重点人群分类统计已检测未检测人次
     */
    @GetMapping("/getLeiBie")
    public Map getLeiBie(IndexVo indexVo) {
        Map map = new HashMap();
        map.put("data", sortAll(indexService.getLeiBie(indexVo))); //总统人数
        map.put("cod", 200);
        map.put("msg", "查询成功");
        return map;
    }


    /**
     * 公共排序方法
     *
     * @param leiBie
     * @return
     */
    private Map sortAll(Map leiBie) {
        if (StringUtils.isNull(leiBie)) {
            return leiBie;
        }
        List<Map> categories = (List) leiBie.get("categories");
        List series = (List) leiBie.get("series");
        Map o = (Map) series.get(0); //总人次
        List zongList = (List) o.get("data"); //总人次数组
        Map w = (Map) series.get(1); //未完成人次
        List weiList = (List) w.get("data");//未完成人次数组
        List<IndexIntVo> IndexIntVoS = new ArrayList<>();
        for (int a = 0; a < categories.size(); a++) {
            IndexIntVo indexIntVo = new IndexIntVo(categories.get(a), Integer.parseInt(weiList.get(a) + ""), Integer.parseInt(zongList.get(a) + ""));
            IndexIntVoS.add(indexIntVo);
        }
        Collections.sort(IndexIntVoS); //排序
        Collections.reverse(IndexIntVoS);//反转
        List categoriess = new ArrayList(); //类别名称
        List yingwancheng = new ArrayList(); //总人次
        List weiwancheng = new ArrayList(); //未完成人次
        for (IndexIntVo indexIntVo : IndexIntVoS) {
            categoriess.add(indexIntVo.getName());
            weiwancheng.add(indexIntVo.getWwc());
            yingwancheng.add(indexIntVo.getZrc());
        }
        Map yingwanchengmap = new HashMap();
        yingwanchengmap.put("data", yingwancheng);
        yingwanchengmap.put("name", "总人次");
        Map weiwanchengmap = new HashMap();
        weiwanchengmap.put("data", weiwancheng);
        weiwanchengmap.put("name", "未完成人次");
        List seriess = new ArrayList();
        seriess.add(yingwanchengmap);
        seriess.add(weiwanchengmap);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("categories", categoriess);
        objectObjectHashMap.put("series", seriess);
        return objectObjectHashMap;
    }

    /**
     * 获取部门是否有下级
     */
    @GetMapping("/getZhongDianRenQun")
    public Map getZhongDianRenQun() {
        Map map = new HashMap();
        map.put("state", indexService.getZhongDianRenQun().size() > 0 ? 0 : 1); //0有1没有
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }

    /**
     * 行业分类
     */
    @GetMapping("/getHnagYeFenLei")
    public Map getHnagYeFenLei(IndexVo indexVo) {
        Map map = new HashMap();
        map.put("data", sortAll(indexService.getHnagYeFenLei(indexVo)));
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }

    /**
     * 默认某一类的时间
     */
    @GetMapping("/getGaoJiSouSuo")
    public Map getGaoJiSouSuo(IndexVo indexVo) {
        Map map = new HashMap();
        map.put("data", indexService.getGaoJiSouSuo(indexVo));
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }

    /**
     * 根据类别搜索，搜索所有的类别
     */
    @GetMapping("/getSuoYouLeiBie")
    public Map getSuoYouLeiBie(IndexVo indexVo) {
        Map map = new HashMap();
        List suoYouLeiBie = indexService.getSuoYouLeiBie(indexVo);
        List list = new ArrayList();
        for (int a = 0; a < suoYouLeiBie.size(); a++) {
            Map o = (Map) suoYouLeiBie.get(a);
            Map value = (Map) o.get("value");
            Map m = new HashMap();
            m.put("name", o.get("name"));
            m.put("value", sortAll(value));
            list.add(m);
        }
        map.put("data", list);
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }


    /**
     * 重点人群信息
     */
    @GetMapping("/getZhongDianRenFenLeiXx")
    public Map getZhongDianRenFenLei(IndexVo indexVo) {
        Map map = new HashMap();
        map.put("data", personService.selectPersonList(new Person(0, 0)));
        map.put("code", 200);
        map.put("msg", "查询成功");
        return map;
    }

    /**
     * 根据不同登录人获取不同的不同列表数据
     * 1.部门上级登录   展示：下级部门管理员
     * 2.包保责任人登录   展示：重点人群
     */

    /**
     * 重点人群信息
     */
    @GetMapping("/getZhongDianrenq")
    public TableDataInfo getZhongDianrenq() {
        List<SysDept> zhongDianRenQun = indexService.getZhongDianRenQun();//是否有下级0有1没有
//        startPage();
        if (zhongDianRenQun.size() > 0) { //查询下级部门管理人员
            //查询下级所有人员
            List<SysUser> sysUsers = sysUserMapper.deptIdUser(zhongDianRenQun);
            return getDataTableXao(sysUsers, 1);
        } else {
            List<Person> list = personService.selectPersonList(new Person(0, 0));
            return getDataTableXao(list, 2);
        }
    }


}
